package com.blockmeta.bbs.businesslibrary.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.blankj.utilcode.util.SpanUtils;
import com.blockmeta.bbs.baselibrary.utils.DimentionsUtil;
import com.blockmeta.bbs.baselibrary.utils.FormatUtil;
import com.blockmeta.bbs.baselibrary.utils.RegexUtil;
import com.blockmeta.bbs.baselibrary.utils.TimeUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.arouter.RouterHelper;
import com.blockmeta.bbs.businesslibrary.arouter.RouterPath;
import com.blockmeta.bbs.businesslibrary.business.UmengEvent;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.net.pojo.InforPOJO;
import com.blockmeta.bbs.businesslibrary.widget.CircleImageView;
import com.blockmeta.bbs.businesslibrary.widget.GlideRoundTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;

import java.text.ParseException;
import java.util.List;

/**
 * Created by author on 2018/1/8.
 */

public class InforListAdapter extends BaseMultiItemQuickAdapter<InforPOJO.ListBean, BaseViewHolder> {

    private Context mContext;
    private boolean isEditStatus;
    private String searchKeyWord;


    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public InforListAdapter(List<InforPOJO.ListBean> data, Context mContext) {
        super(data);
        //三张
        addItemType(InforPOJO.ListBean.Type3, R.layout.item_news_type1);
        //一张
        addItemType(InforPOJO.ListBean.Type1Normal, R.layout.item_news_type2);
        //创投
        addItemType(InforPOJO.ListBean.Type1Invest, R.layout.item_news_type2);
        //专题
        addItemType(InforPOJO.ListBean.Type1Topic, R.layout.item_news_chaintopic);
        //视频
        addItemType(InforPOJO.ListBean.Type1Video, R.layout.item_news_type_video);
        //周刊
        addItemType(InforPOJO.ListBean.Type1Week, R.layout.item_news_chaintopic);
        this.mContext = mContext;
    }

    public void setSearchKeyWord(String key, String searchKeyWord) {
        if("keyword".equals(key)) {
            this.searchKeyWord = searchKeyWord;
            //如果是搜索的需要替换布局
            addItemType(InforPOJO.ListBean.Type1Normal, R.layout.item_news_type2_search);
        }
    }

    public void setEditStatus(boolean editStatus) {
        isEditStatus = editStatus;
    }

    @Override
    protected void convert(BaseViewHolder helper, InforPOJO.ListBean item) {

        switch (helper.getItemViewType()) {
            case InforPOJO.ListBean.Type3:
                //三张图片
                initTypeThreePic(helper, item);
                break;
            case InforPOJO.ListBean.Type1Normal:
                //一张
                initTypeNormal(helper, item);
                break;
            case InforPOJO.ListBean.Type1Invest:
                //创投
                initTypeInvest(helper, item);
                break;
            case InforPOJO.ListBean.Type1Topic:
                //专题
                initTypeTopic(helper, item);
                break;
            case InforPOJO.ListBean.Type1Video:
                //视频
                initTypeVideo(helper, item);
                break;
            case InforPOJO.ListBean.Type1Week:
                //周刊
                initTypeWeek(helper, item);
                break;
            default:
                break;
        }

    }

    private void initTypeWeek(BaseViewHolder helper, final InforPOJO.ListBean item) {

    }

    private void initTypeVideo(BaseViewHolder helper, final InforPOJO.ListBean item) {
        helper.setText(R.id.video_title, RegexUtil.getHightLight(item.getTitle(), searchKeyWord));
        if (!TextUtils.isEmpty(item.getImage())) {
            Picasso.with(mContext).load(item.getImage()).error(R.drawable.yoona)
                    .placeholder(R.drawable.yoona).into((ImageView) helper.getView(R.id.video_img));
        }
        if (!TextUtils.isEmpty(item.getAuthor_info().getAvatar())) {
            Picasso.with(mContext).load(item.getAuthor_info().getAvatar()).error(R.drawable.photo_default)
                    .placeholder(R.drawable.photo_default).into((ImageView) helper.getView(R.id.video_avatar));
        }

        try {
            String dateToStamp = FormatUtil.dateToStampHMS(item.getPost_date_format());
            helper.setText(R.id.video_time, TimeUtil.InforMationTime(FormatUtil.parseInt(dateToStamp)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        helper.setText(R.id.video_from, item.getAuthor_info().getDisplay_name());
        helper.setText(R.id.video_views, item.getViews());
        helper.setText(R.id.video_reply, item.getReplies()).addOnClickListener(R.id.video_main);

        helper.getView(R.id.video_main).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpToTarget(item);
            }
        });

    }

    private void initTypeTopic(BaseViewHolder helper, final InforPOJO.ListBean item) {


        String title = item.getTitle().trim();
        final String replace = title.replace("】", "》").replace("【", "《").replace("专题", "");
        String[] split = replace.split("：");
        try {
            helper.setText(R.id.chaintopic_title, RegexUtil.getHightLight(split[0], searchKeyWord));
            helper.setText(R.id.chaintopic_desc, RegexUtil.getHightLight(split[1], searchKeyWord));
        } catch (Exception e) {
            helper.setText(R.id.chaintopic_title, RegexUtil.getHightLight(split[0], searchKeyWord));
            helper.setText(R.id.chaintopic_desc, RegexUtil.getHightLight(item.getDesc(), searchKeyWord));
        }

        helper.setText(R.id.chaintopic_views, item.getViews() + "");
        helper.setText(R.id.chaintopic_comment, item.getReplies() + "");
        if (TextUtils.isEmpty(item.getExtra().getTheme_num())) {
            helper.setText(R.id.chaintopic_time, "链专题");
        } else {
            helper.setText(R.id.chaintopic_time, new SpanUtils().append("第 ").append(item.getExtra().getTheme_num() + "").setFontSize(DimentionsUtil.dp2px(mContext, 25)).append(" 期").create());
        }
        RelativeLayout click = helper.getView(R.id.chaintopic_click);
        click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RouterHelper.startActivity(mContext,RouterPath.CHAIN_TOPIC_ACTIVITY,item.getId()+"");
            }
        });

    }

    /**
     * 创投
     *
     * @param helper
     * @param item
     */
    private void initTypeInvest(BaseViewHolder helper, final InforPOJO.ListBean item) {
        TextView tv_inv = helper.getView(R.id.invest_money);
        tv_inv.setVisibility(View.VISIBLE);
        tv_inv.setText(item.getExtra().getAmount());

        if (!TextUtils.isEmpty(item.getImage())) {
            Glide.with(mContext).load(item.getImage()).placeholder(R.drawable.yoona).into((ImageView) helper.getView(R.id.item_news_type2_img));
        }
        helper.setText(R.id.item_news_type2_title, RegexUtil.getHightLight(item.getTitle(), searchKeyWord));
        CircleImageView cirImg = helper.getView(R.id.item_news_type2_from);
        if (!TextUtils.isEmpty(item.getAuthor_info().getAvatar())) {
            Picasso.with(mContext).load(item.getAuthor_info().getAvatar()).placeholder(R.drawable.yoona).into(cirImg);
        }

        try {
            String dateToStamp = FormatUtil.dateToStampHMS(item.getPost_date_format());
            helper.setText(R.id.item_news_type2_time, TimeUtil.InforMationTime(Integer.parseInt(dateToStamp)));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        helper.setText(R.id.item_news_type2_views, item.getViews());
        helper.setText(R.id.item_news_type2_reply, item.getReplies());
        ToggleButton toggleButton = helper.getView(R.id.tg_type2_selector);
        if (isEditStatus) {
            toggleButton.setChecked(false);
            toggleButton.setVisibility(View.VISIBLE);
        } else {
            toggleButton.setVisibility(View.GONE);
        }
        item.setSelected(toggleButton.isSelected());
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                item.setSelected(isChecked);
            }
        });

        LinearLayout main = helper.getView(R.id.item_news_type2_main);
        main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpToTarget(item);
            }
        });

    }

    /**
     * 三张图片
     *
     * @param helper
     * @param item
     */
    private void initTypeThreePic(BaseViewHolder helper, final InforPOJO.ListBean item) {
        if (!TextUtils.isEmpty(item.getImages().get(0))) {
            Glide.with(mContext).load(item.getImages().get(0)).into((ImageView) helper.getView(R.id.type1_img1));
            Glide.with(mContext).load(item.getImages().get(1)).into((ImageView) helper.getView(R.id.type1_img2));
            Glide.with(mContext).load(item.getImages().get(2)).into((ImageView) helper.getView(R.id.type1_img3));
        }
        helper.setText(R.id.type1_title, RegexUtil.getHightLight(item.getTitle(), searchKeyWord));
        helper.setText(R.id.type1_from, item.getAuthor_info().getName());
        CircleImageView cirImg = helper.getView(R.id.item_news_type1_from);

        if (!TextUtils.isEmpty(item.getAuthor_info().getAvatar())) {
            Picasso.with(mContext).load(item.getAuthor_info().getAvatar()).placeholder(R.drawable.yoona).into(cirImg);
        }
        try {
            String dateToStamp = FormatUtil.dateToStampHMS(item.getPost_date_format());
            helper.setText(R.id.type1_time, TimeUtil.InforMationTime(Integer.parseInt(dateToStamp)));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        helper.setText(R.id.type1_views, item.getViews());
        helper.setText(R.id.type1_reply, item.getReplies());
        ToggleButton toggleButton = helper.getView(R.id.tg_type1_selector);
        if (isEditStatus) {
            toggleButton.setChecked(false);
            toggleButton.setVisibility(View.VISIBLE);
        } else {
            toggleButton.setVisibility(View.GONE);
        }
        item.setSelected(toggleButton.isSelected());
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                item.setSelected(isChecked);
            }
        });

        LinearLayout main = helper.getView(R.id.item_news_type1_main);
        main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpToTarget(item);
            }
        });

    }

    /**
     * 一张图片普通
     *
     * @param helper
     * @param item
     */
    private void initTypeNormal(BaseViewHolder helper, final InforPOJO.ListBean item) {
        if (item.getImages() != null && !TextUtils.isEmpty(item.getImages().get(0))) {
            Glide.with(mContext).load(item.getImages().get(0)).placeholder(R.drawable.yoona).into((ImageView) helper.getView(R.id.item_news_type2_img));
        } else if (!TextUtils.isEmpty(item.getImage())) {
            Glide.with(mContext).load(item.getImage()).placeholder(R.drawable.yoona).into((ImageView) helper.getView(R.id.item_news_type2_img));
        } else {
            helper.setImageResource(R.id.item_news_type2_img, R.drawable.yoona);
        }
        helper.setText(R.id.item_news_type2_title, RegexUtil.getHightLight(item.getTitle(), searchKeyWord));
        CircleImageView cirImg = helper.getView(R.id.item_news_type2_from);
        if (!TextUtils.isEmpty(item.getAuthor_info().getAvatar())) {
            Picasso.with(mContext).load(item.getAuthor_info().getAvatar()).placeholder(R.drawable.photo_default).into(cirImg);
        }
        if(helper.getView(R.id.item_news_content) != null) {
            helper.setText(R.id.item_news_content, RegexUtil.getHightLight(item.getContent(), searchKeyWord));
        }
        try {
            String dateToStamp = FormatUtil.dateToStampHMS(item.getPost_date_format());
            helper.setText(R.id.item_news_type2_time, TimeUtil.InforMationTime(Integer.parseInt(dateToStamp)));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        helper.setText(R.id.item_news_type2_views, item.getViews() + "");
        helper.setText(R.id.item_news_type2_reply, item.getReplies() + "");
        ToggleButton toggleButton = helper.getView(R.id.tg_type2_selector);
        if (isEditStatus) {
            toggleButton.setChecked(false);
            toggleButton.setVisibility(View.VISIBLE);
        } else {
            toggleButton.setVisibility(View.GONE);
        }
        item.setSelected(toggleButton.isSelected());
        toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                item.setSelected(isChecked);
            }
        });

        LinearLayout main = helper.getView(R.id.item_news_type2_main);
        main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpToTarget(item);
            }
        });
    }


    private void jumpToTarget(InforPOJO.ListBean item) {
        if (isEditStatus) return;
        if (!TextUtils.isEmpty(item.getPost_name())) {
            //内站数据
            UmengEvent.eventStatistics(mContext, Constant.U_INFO_DETAIL, null);
            RouterHelper.startActivity(mContext, RouterPath.NEWS_INNER_DETAIL_ACTIVITY
            ,item.getAuthor_info().getName()
            ,item.getId()+"");
        } else {//外站
            RouterHelper.startActivity(mContext,RouterPath.NEWS_DETAIL_OUT_ACTIVITY
            ,item.getId()+"");
        }
    }

}
